﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using RedHouse.Data.Entities;
using RedHouse.Data.Common;
using RedHouse.Data;
using RedHouse.Web.common;

namespace RedHouse.Web
{
    public partial class Promotions : PageBase
    {
        INewsService _newsService;
        ILanguageService _languageService;
        public RedHouseNews NewsDetail { get; set; }
        public List<RedHouseNews> NewsList { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (NewsList == null)
            {
                NewsList = new List<RedHouseNews>();
            }
            _newsService = new NewsService();
            _languageService = new LanguageService();
            //if (!IsPostBack)
            //{                
            Initialize();
            if (Request.QueryString["id"] != null)
            {
                ucDetail.Visible = true;
                ucList.Visible = false;

                Guid id = new Guid(Request.QueryString["id"]);
                RedHouseNews news = NewsList.Where(n => n.Id == id).FirstOrDefault();
                NewsDetail = news;

                ClientScriptManager cs = Page.ClientScript;
                String csname = "PopupScript";
                Type cstype = this.GetType();
                if (!cs.IsStartupScriptRegistered(cstype, csname))
                {
                    StringBuilder script = new StringBuilder();
                    script.Append("<script type=text/javascript>");
                    script.Append("$(document).ready(function() { $('#accordion-menu li ul li a').removeClass('active'); $('#accordion-menu li ul li a').removeClass('active'); $('.level-1').removeClass('active'); $('.level-1').parent().find('ul').css('display','none'); $('#" + NewsDetail.Id + "').parent().parent().parent().find('a').addClass('active'); $('#" + NewsDetail.Id + "').parent().parent().parent().find('ul').css('display','block'); $('#accordion-menu li ul li a').removeClass('active'); $('#" + NewsDetail.Id + "').addClass('active'); });");
                    script.Append("</script>");

                    cs.RegisterStartupScript(cstype, csname, script.ToString());
                }
                
                ucDetail.Promotion = news;
            }
            else
            {
                ucDetail.Visible = false;
                ucList.Visible = true;

                ClientScriptManager cs = Page.ClientScript;
                String csname = "PopupScript";
                Type cstype = this.GetType();
                if (!cs.IsStartupScriptRegistered(cstype, csname))
                {
                    StringBuilder script = new StringBuilder();
                    script.Append("<script type=text/javascript>");
                    script.Append("$(document).ready(function() { $('#accordion-menu li ul li a').removeClass('active'); $('.level-1').removeClass('active'); $('.level-1').parent().find('ul').css('display','none'); $('.level-1:first').addClass('active'); $('.level-1:first').parent().find('ul').css('display','block'); });");
                    script.Append("</script>");

                    cs.RegisterStartupScript(cstype, csname, script.ToString());
                }

                if (Request.QueryString["view"] != null && Request.QueryString["view"].ToString() == "2")
                {
                    ucList.NewsList = NewsList.Where(n => n.EndDate < DateTime.Now).OrderByDescending(n => n.CreatedDate).ToList();
                }
                else
                {
                    ucList.NewsList = NewsList.Where(n => n.EndDate >= DateTime.Now).OrderByDescending(n => n.CreatedDate).ToList();
                }
            }
        }

        private void Initialize()
        {
            ucDetail.Visible = false;
            ucList.Visible = false;


            NewsList = _newsService.GetAllNewsByGroup(RedHouseGroup.GroupList[0].Value)
                                   .Where(n => n.Category.ToLower().Equals("promotion"))
                                   .OrderByDescending(n => n.CreatedDate).ToList();
            if (Session["location"] != null)
            {
                int location = int.Parse(Session["location"].ToString());
                NewsList = NewsList.Where(n => n.LocationId == location || n.LocationId==3).OrderByDescending(p => p.NewsDetail.CreatedDate).ToList();
            }

            Language language = null;
            if (Session["Culture"] != null && Session["Culture"].ToString() == "en-US")
            {
                language = _languageService.GetAllLanguage().Where(l => l.Description == "English").FirstOrDefault();
            }
            else
            {
                language = _languageService.GetAllLanguage().Where(l => l.Description != "English").FirstOrDefault();
            }

            if (language != null)
            {
                NewsList = NewsList.Where(n => n.NewsDetail.LangID == language.ID).ToList();
            }
        }

        
    }
}